<!DOCTYPE html>
<html lang="zh-cn">
<!-- Form表单提交测试 -->
<head>
	<meta charset="utf-8" />
	<title>光标位置测试</title>
	<script>
		//输入框获取光标
		function getCursorPosition(element) {

			let cursorPos = 0;
			if (document.selection) {//IE
				var selectRange = document.selection.createRange();
				selectRange.moveStart('character', -element.value.length);
				cursorPos = selectRange.text.length;
			} else if (element.selectionStart || element.selectionStart == '0') {
				cursorPos = element.selectionStart;
			}
			
			return cursorPos
		}
	</script>
</head>

<body>
	<input id="input" />
	<button onclick="getCursorPosition(input)">获取光标位置</button>

</body>

</html>